/**
 * 
 */
package com.jeecms.wicool.dao.distribution.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.distribution.DistributionUserDao;
import com.jeecms.wicool.entity.distribution.DistributionUser;

/**
 * @author Administrator
 *
 */
public class DistributionUserDaoImpl extends
		HibernateBaseDao<DistributionUser, Integer> implements
		DistributionUserDao {

	@Override
	public Pagination getPage(String name,String mobile, int pageNo, int pageSize) {
		Finder f = Finder.create("select bean from DistributionUser bean where 1=1 ");
		if (!StringUtils.isBlank(name)){
			f.append(" and bean.name like :name");
			f.setParam("name", "%" + name + "%");
		}
		if (!StringUtils.isBlank(mobile)){
			f.append(" and bean.mobile like :mobile");
			f.setParam("mobile", "%" + mobile + "%");
		}
		f.append(" order by bean.id desc");
		return find(f, pageNo, pageSize);
	}

	@Override
	public DistributionUser findById(Integer id) {
		return get(id);
	}

	@Override
	public DistributionUser save(DistributionUser obj) {
		getSession().save(obj);
		getSession().flush();
		getSession().clear();
		return obj;
	}

	@Override
	public DistributionUser deleteById(Integer id) {
		DistributionUser entity = get(id);
		if(entity != null){
			getSession().delete(entity);
		}
		return entity;
	}

	@Override
	public List<DistributionUser> deleteByIds(Integer[] ids) {
		if (null ==ids || ids.length ==0) {
			return null;
		}
		
		List<DistributionUser> list = new ArrayList<DistributionUser>();
		for (Integer id : ids) {
			list.add(deleteById(id));
		}
		getSession().flush();
		getSession().clear();
		return list;
	}

	@Override
	public DistributionUser update(DistributionUser obj) {
		if (obj != null) {
			getSession().update(obj);
			getSession().flush();
			getSession().clear();
		}
		return obj;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<DistributionUser> getAll() {
		Finder f = Finder.create("select bean from DistributionUser bean");
		return find(f);
	}

	@Override
	protected Class<DistributionUser> getEntityClass() {
		return DistributionUser.class;
	}

}
